from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 初始化浏览器驱动
driver = webdriver.Chrome()

# 设置隐式等待时间为10秒
driver.implicitly_wait(10)

# 访问百度首页
url = "https://www.baidu.com"
driver.get(url)
driver.maximize_window()

try:
    # 查找“新闻”链接并点击
    news_link = driver.find_element(By.LINK_TEXT, "新闻")
    news_link.click()

    # 等待新页面加载并截图
    time.sleep(3)
    driver.save_screenshot("baidu_news2_page.png")
    print("截图已保存")

except Exception as e:
    print(f"操作失败：{e}")

finally:
    # 关闭浏览器
    driver.quit()